package config

import (
	"github.com/BurntSushi/toml"
	"io/ioutil"
)

type tomlFile struct {
	bytes []byte
	name string
}

func (tf *tomlFile) Write (b []byte) (n int,err error) {
	tf.bytes = make([]byte,len(b))
	n = copy(tf.bytes,b)
	return
}

func (tf *tomlFile) Read (b []byte) (n int,err error) {
	n = copy(b,tf.bytes)
	return
}


func (tf *tomlFile) Unmarshal(v interface{}) error {
	err := toml.Unmarshal(tf.bytes,v)
	if err != nil {
		return newError(err.Error())
	}
	return nil
}

func (tf *tomlFile) Marshal (v interface{}) error {
	encoder := toml.NewEncoder(tf)
	encoder.Indent = "\t"
	err := encoder.Encode(v)
	if err != nil {
		return err
	}

	err = ioutil.WriteFile(tf.name,tf.bytes,0755)
	return err
}